package com.qf.service.impl;

import com.qf.VO.ProductBatchesVO;
import com.qf.VO.Stu;
import com.qf.mapper.FarmMapper;
import com.qf.mapper.ProductBatchesMapper;
import com.qf.mapper.SysUserMapper;
import com.qf.mapper.TaskMapper;
import com.qf.model.Farm;
import com.qf.model.ProductBatches;
import com.qf.model.SysUser;
import com.qf.model.TTask;
import com.qf.page.PageResult;
import com.qf.page.QueryPageBean;
import com.qf.service.ProductBatchesService;
import com.qf.service.SysUserService;
import com.qf.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;

@Service
public class ProductBatchesServiceImpl implements ProductBatchesService {
    @Autowired
    private ProductBatchesMapper productBatchesMapper;
    @Autowired
    private FarmMapper farmMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private TaskMapper taskMapper;

    @Override
    @Transactional
    public int add(ProductBatchesVO productBatchesVO) {
        productBatchesMapper.add(productBatchesVO);
        Integer producterId = productBatchesVO.getProducterId();
        Integer productBatchesId = productBatchesMapper.findByName(productBatchesVO.getProductName()).getId();
        List<Stu> taskContents = productBatchesVO.getContents();
        System.out.println("taskContents--->"+taskContents);
        int i = 0;
        for (Stu taskContent : taskContents) {
            TTask task = new TTask();
            task.setSysUserId(productBatchesVO.getSysUserId());
            task.setFarmId(productBatchesVO.getFarmId());
            task.setState(1);
            task.setContent(taskContent.getValue());
            task.setProducterId(producterId);
            task.setCreateDate(new Date());
            task.setProductBatchesId(productBatchesId);
            int add = taskMapper.add(task);
            if (add>0){
                i++;
            }
        }
        if (i >= taskContents.size()){
            return i;
        }
        return 0;
    }

    @Override
    public ProductBatches findById(int id) {
        return productBatchesMapper.findById(id);
    }


    @Override
    @Transactional
    public int edit(ProductBatches productBatches) {

        int i = productBatchesMapper.edit(productBatches);
        return i;
    }


    @Override
    @Transactional
    public int delete(int id) {
        return productBatchesMapper.delete(id);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = null;
        if (principal instanceof UserDetails) {
            username = ((UserDetails)principal).getUsername();
        } else {
            username = principal.toString();
        }
        System.out.println("username---------->"+username);
        int userId = sysUserMapper.findUserByUsername(username).getId();



        Integer CurrentPage = queryPageBean.getCurrentPage();
        Integer PageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageResult pageResult = new PageResult();
        List<ProductBatches> rows = productBatchesMapper.findPage((CurrentPage - 1) * PageSize, PageSize, queryString,userId);
        for (ProductBatches row : rows) {
            Farm farm = farmMapper.findById(row.getFarmId());
            SysUser sysUser = sysUserMapper.findById(farm.getUserId());
            row.setSysUser(sysUser);
            row.setFarm(farm);
        }
        Long total = productBatchesMapper.count(queryString,userId);
        System.out.println("rows"+rows);
        pageResult.setRows(rows);
        pageResult.setTotal(total);
        return pageResult;
    }


}
